<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Document</title>
    <script src='../vue.js'></script>
    <style>
        .app{
            height: 100%;
            width: 100%;
           background-color: #ccc;
        }
        .box {
            margin: 100px auto;
            width: 400px;
            /* height: 100px; */
            background-color: transparent;
            border: 1px solid #39c5bb;
            border-radius: 5px;
            padding: 10px;
        }
        .box>input{
                width: 100%;
                margin: 0;
                padding: 10px;
                box-sizing: border-box;
                border-radius: 2px;
            }
            .myitemtodo{
                margin: 10px;
                font-weight: 800;
            }

    </style>
</head>

<body>
    <div id='app'>
        <div class="box">
            <!-- <input type="text" placeholder="请输入待办事项" v-model="contentitem" @keyup.enter="addTodo"/> -->
             <myinput :contentitem="contentitem"></myinput>
            <template v-for="(item,index) in mylisttodo" :key="index">
                <div class="myitemtodo">
                    <input type="checkbox" v-model="item.flag">
                    {{item.thing}}
                </div>
                <hr>
            </template>
            <div class="checkall">
                <input type="checkbox" v-model="allChecked" >
                全选：已完成{{checkcount}}件 / 总计：{{mylisttodo.length}}件
            </div>
        </div>
    </div>
</body>
<template id="myinput">
   <div style="display: flex;width: 100%;">
    <input @keyup.enter="add" style="flex:1" type="text" placeholder="请输入待办事项" v-model="thing" @keyup.enter="add"/>
    <button v-on:click="add">添加</button>
   </div>
</template>
<script>
    const myinput = {
        template: '#myinput',
        props:["contentitem"],
        data() {
            return {
                thing: ''
            }
        },
        methods: {
            add() {
                if (this.thing.trim().length == 0) {
                    alert('请输入待办事项')
                    return
                }
                this.$parent.contentitem=this.thing
                this.thing = ''
            }
        }
    }
    const app = {
        data() {
            return {
                contentitem:"",
                mylisttodo:[
                    {
                        flag:true,
                        thing:"吃饭"
                    },
                    {
                        flag:false,
                        thing:"睡觉"
                    },
                    {
                        flag:false,
                        thing:"打游戏"
                    },
                    {
                        flag:false,
                        thing:"好好睡觉"
                    }
                ]
            }
        },
        methods: {
            addTodo(){
                // console.log(111);
                
                this.mylisttodo.unshift({
                    flag:false,
                    thing:this.contentitem,
                })
            }
        },
        computed:{
            checkcount(){
                return this.mylisttodo.filter(item => item.flag).length
            },
            // 计算是否全选
            isallcheck(){
                return this.mylisttodo.length > 0 && 
                       this.mylisttodo.every(item => item.flag);
            },
            // 全选复选框的 getter 和 setter
            allChecked: {
                get() {
                    return this.isallcheck;
                },
                set(value) {
                    // 设置所有项的 flag 状态
                    this.mylisttodo.forEach(item => {
                        item.flag = value;
                    });
                }
            }
        },
        components:{
            myinput
        }
    }
    const vm = Vue.createApp(app).mount('#app')
</script>

</html>